package service

import (
	"ksd-social-api/commons/base/service"
	context3 "ksd-social-api/modules/msg/model/context"
	"ksd-social-api/modules/resume/model"
	umodel "ksd-social-api/modules/user/model"
)

type ResumeCodeService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *ResumeCodeService) DuihuanResume(code string, resumeid uint64, userId uint64, systemId uint64, systemName string) bool {
	resumeCode := resumeCodeMapper.CountResumeCode(code, resumeid, systemId)
	// 如果还没兑换，就开始兑换，生成小册订单，同时删除订单
	if resumeCode > 0 {
		// 删除兑换的订单
		resumeCodeMapper.UpdateResumeCode(code, resumeid, systemId)
		// 开始保存订单
		service.saveUserResumeOrder(code, resumeid, userId, systemId, systemName)
		// 兑换成功
		return true
	}
	return false
}

/**
 * 保存用户小册订单
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *ResumeCodeService) saveUserResumeOrder(code string, resumeId uint64, userId uint64, systemId uint64, systemName string) {
	isBuyFlag := resumeMapper.CountUserBuyResumeIdNo(userId, resumeId, systemId)
	if isBuyFlag > 0 {
		// 根据小册id查询小册信息
		resume, _ := resumeMapper.GetById(resumeId, systemId)
		resumeNew := model.Resume{}
		resumeNew.Id = resumeId
		OutTradeNo := service.GetSnowWorkerIdString(8)
		resumeNew.TradeNo = OutTradeNo
		resumeNew.PayMethod = "兑换码"
		resumeNew.Code = code
		resumeNew.SystemId = systemId
		resumeNew.Price = resume.Price
		// 保存订单的接口
		resumeMapper.UpdateResume(&resumeNew)
		// 增加会员

		// 根据课程id查询课程信息
		userVipVo := userVipMapper.GetUserVipDetail(resume.RType, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)
		var userBuyVip umodel.UserBuyVip
		userBuyVip.SystemId = systemId
		userBuyVip.SystemName = systemName
		// 处理返回结果
		userBuyVip.VipId = resume.RType
		userBuyVip.Title = userVipVo.Title
		userBuyVip.Description = userVipVo.Note

		userBuyVip.UserId = userId
		userBuyVip.Username = userVo.Username
		userBuyVip.Avatar = userVo.Avatar
		userBuyVip.Uuid = userVo.Uuid
		userBuyVip.Phone = userVo.Telephone
		userBuyVip.Address = userVo.Address
		userBuyVip.Nickname = userVo.Nickname

		userBuyVip.Code = ""
		userBuyVip.PayMethod = 3
		userBuyVip.PayMethodName = "兑换码"
		userBuyVip.Tradeno = OutTradeNo
		userBuyVip.Orderno = OutTradeNo
		userBuyVip.OrderJson = ""
		userBuyVip.Price = userVipVo.Price
		// 保存订单的接口
		userBuyVipMapper.SaveUserBuyVip(userBuyVip)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = resume.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com/resume"
		messageMeService.SaveMessageMeBuyResume(&messageMeContext)
	}
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *ResumeCodeService) DuihuanCoinResume(resumeId uint64, userId uint64, systemId uint64, systemName string) bool {
	// 兑换成功
	return true
}
